
#mkdir build &&cd build
# 交叉编译执行  cmake ../ -DCMAKE_TOOLCHAIN_FILE=../cortex_m.cmake
# 调试版本      cmake ../ -DCMAKE_BUILD_TYPE=debug -DCMAKE_TOOLCHAIN_FILE=../cortex_m.cmake

project(n32g455 C ASM)

file(GLOB_RECURSE sources CONFIGURE_DEPENDS
    firmware/*.c
    firmware/*gcc.s
    projects/n32g45x_EVAL/bsp/*.c
    USART/*.c
    USB/*.c
)

message(123)
message(STATUS "${sources}.")

#添加头文件搜索路径
include_directories(
    firmware/CMSIS
    firmware/CMSIS/device
    firmware/CMSIS/core
    firmware/n32g45x_std_periph_driver/inc
    projects/n32g45x_EVAL/bsp/inc/
    USART/Printf/inc
    USB/tusb
)

add_compile_definitions(
    N32G45X
    USE_STDPERIPH_DRIVER
    USE_FULL_ASSERT
)

# 添加USB编译宏
add_compile_definitions(
    CFG_TUSB_MCU=8800
    TUP_DCD_ENDPOINT_MAX=8
)

add_executable(
    main.elf ${sources}
)

# 生成hex文件和tcl文件
#   如果使用分步骤处理命令
#   openocd 打开调试端口
#       openocd -f link.cfg -f xxxmcu.cfg -f mcu_flash.tcl -f write_flash.tcl

#   然后使用telnet localhost 4444, 然后输入以下指令
#       reset
#       halt
#       xflash-unlock
#       xflash-program-start
#           source main_elf.tcl
#       xflash-program-end

#   最后打开gdb调试
#       arm-none-eabi-gdb main.elf
#       target remote :3333
#       set $pc=reset_handle
#   注:有关reset_handle, 是源码的handle路径
add_custom_command(
    TARGET main.elf POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -O ihex main.elf main.hex
    COMMAND lua.exe ${CMAKE_CURRENT_LIST_DIR}/ihex2tcl.lua main.hex >main_elf.tcl
)